<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model;


use app\common\model\BaseModel;

/**
 * 佣金日志
 * @property $id   id
 * @property $sn   订单编号
 * @property $merchant_id   商户
 * @property $amount   金额
 * @property $create_time   创建时间
 * @property $update_time   更新时间
 * @property $delete_time   删除时间
 * @property $before   变更前
 * @property $after   变更后
 * @property $remark   说明
 * @property Merchant   $merchant   
*/

class CommissionLog extends BaseModel
{

    protected $name = 'commission_log';



    public function merchant(){
        return $this->belongsTo(Merchant::class,'merchant_id');
    }


    public static function log(Merchant $merchant, float $amount, string $remark = '')
    {
        $before = $merchant->commission;
        $merchant->commission = bcadd($merchant->commission, $amount, 2);
        $after = $merchant->commission;
        $merchant->save();
        $sn = generate_sn(CommissionLog::class, 'sn');
        $merchant->commissionlog()->save([
            'sn'     => $sn,
            'amount' => abs($amount),
            'before' => $before,
            'after'  => $after,
            'remark' => $remark,
        ]);
    }

}